import config
import os
import read_mssql_to_file
import read_mysql_to_file

read_tab_mapping_file = open(config.tab_mapping_file, 'r')
try:
    while True:
        line = read_tab_mapping_file.readline().strip('\n')
        if line:
            mapping_elem = line.split("\t")
            tab_dir_path = config.export_dir+"/"+mapping_elem[0]
            os.mkdir(tab_dir_path)
            erp_to_bak_path = tab_dir_path+"/"+mapping_elem[0]+"2"+mapping_elem[1]
            bak_to_aps_path = tab_dir_path+"/"+mapping_elem[1]+"2"+mapping_elem[2]
            os.mkdir(erp_to_bak_path)
            os.mkdir(bak_to_aps_path)
            erp_db_cols_file = tab_dir_path + "/" + mapping_elem[0]
            bak_db_cols_file = tab_dir_path + "/" + mapping_elem[1]
            aps_db_cols_file = tab_dir_path + "/" + mapping_elem[2]
            read_mssql_to_file.write_to_file(erp_db_cols_file, mapping_elem[0])
            read_mysql_to_file.write_to_file( bak_db_cols_file, "kaer", mapping_elem[1])
            read_mysql_to_file.write_to_file( aps_db_cols_file, "tb_kaer", mapping_elem[2])
        else:
            break
finally:
    read_tab_mapping_file.close()